package com.chuji.crmcj.act;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.NoUserAdapter;
import com.chuji.crmcj.utils.ConstantValue;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 新任务
 * Created by w.pitt on 2016/1/12.
 */
public class NewtaskActivity extends BaseActivity implements View.OnClickListener {
    private ImageView mIv_confirm;
    private ImageView mIv_error;
    private RelativeLayout mRl_task;
    private int extra;
    private LinearLayout mLlOption;
    private TextView mFirst;
    private TextView mSecond;
    private String invalid;
    private String salerID;
    private String customerID;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_new_task);
        mRl_task = (RelativeLayout) findViewById(R.id.rl_task);
        mIv_confirm = (ImageView) findViewById(R.id.iv_confirm);
        mIv_error = (ImageView) findViewById(R.id.iv_error);
        mLlOption = (LinearLayout) findViewById(R.id.ll_option);
        mFirst = (TextView) findViewById(R.id.first);
        mSecond = (TextView) findViewById(R.id.second);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.act_out);
    }

    @Override
    protected void initData() {
        salerID = SPUtils.getString(UIUtils.getContext(), "salerID", "");
        Intent intent = getIntent();
        customerID = intent.getStringExtra("CustomerID");

        extra = intent.getIntExtra("SOURCE", 0);
        if (extra == ConstantValue.REDEAL) {
            mIv_confirm.setImageDrawable(UIUtils.getDrawable(R.drawable.getcat));
            mIv_error.setImageDrawable(UIUtils.getDrawable(R.drawable.order));
            mFirst.setText("提车");
            mSecond.setText("订单");
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        startAnimation(-1f, 0f);
    }

    private void startAnimation(float start, float end) {
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0f,
                Animation.RELATIVE_TO_PARENT, start, Animation.RELATIVE_TO_PARENT, end);
        ta.setDuration(400);
        ta.setFillAfter(true);
        mLlOption.setAnimation(ta);
    }

    @Override
    protected void initListener() {
        mIv_confirm.setOnClickListener(this);
        mIv_error.setOnClickListener(this);
        mRl_task.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_confirm:
                startAnimation(0f, -20f);
                finishAnimation();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (extra == ConstantValue.REDEAL) {
                            Intent intent = new Intent(NewtaskActivity.this, OrderWriteActivity.class);
                            intent.putExtra("CustomerID", customerID);
                            startActivity(intent);
                        } else {
                            reqInvalid(0);
                            startActivity(new Intent(NewtaskActivity.this, ReceptionActivity.class));
                            finish();
                        }
                    }
                }, 0);
                break;
            case R.id.iv_error:
                if (extra == ConstantValue.REDEAL) {
                    Intent intent = new Intent(NewtaskActivity.this, OrderTurnoverWriteActivity.class);
                    intent.putExtra("CustomerID", customerID);
                    startActivity(intent);
                } else {
                    showErrorDialog();
                }
                break;

            case R.id.rl_task:
                startAnimation(0f, -1f);
                SPUtils.saveInt(UIUtils.getContext(), "IsShow", -1);
                finishAnimation();
                break;
        }
    }

    private String option;

    private void showErrorDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_invalid, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSure = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        ListView mLvReason = (ListView) view.findViewById(R.id.lv_reason);

        NoUserAdapter adapter = new NoUserAdapter(0);
        mLvReason.setAdapter(adapter);
//        RadioGroup mRgCall = (RadioGroup) view.findViewById(R.id.call_radio);
//        mRgCall.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
//                switch (checkedId) {
//                    case R.id.rb_restroom:
//                        option = "上洗手间";
//                        break;
//                    case R.id.rb_takeaway:
//                        option = "送快递、外卖";
//                        break;
//                    case R.id.rb_not_bug:
//                        option = "非购车业务";
//                        break;
//                }
//            }
//        });

        mBtSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invalid = SPUtils.getString(UIUtils.getContext(), "invalid", "");
                if (TextUtils.isEmpty(invalid)) {
                    UIUtils.showToastSafe("请选择无效原因");
                } else {
                    reqInvalid(1);
                    dialog.dismiss();
                }
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    private void reqInvalid(final int state) {
        /* @State 1无效 0有效 */
//        String strURL = String.format(UrlUtils.ValidPassengerFlow +
//                "&SalesID=%s&State=%d&Reason=%s", salerID, state, invalid);
//        volleyRequest(strURL, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String s) {
//                UIUtils.runInMainThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (state == 0) {
//                            UIUtils.showToastSafe("已设为无效客户");
//                        } else {
//                            //有效客户
//                        }
//                    }
//                });
//            }
//        });

        String strURL = String.format(UrlUtils.ValidPassengerFlow);
        Map<String, String> map = new HashMap<String, String>();
        map.put("SalesID", salerID);
        map.put("State", String.valueOf(state));
        map.put("Reason", invalid);
        postRequest(strURL, map, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                if (state == 1) {
                    UIUtils.showToastSafe("已设为无效客户");
                    SPUtils.saveInt(UIUtils.getContext(), "IsShow", 1);
                    SPUtils.saveInt(UIUtils.getContext(), "Free", 1);
                    finishAnimation();
                } else {
                    UIUtils.showToastSafe("已设为有效客户");
                    //有效客户
                    SPUtils.saveInt(UIUtils.getContext(), "IsShow", 1);
                    SPUtils.saveInt(UIUtils.getContext(), "Free", 0);
                }
            }
        });
    }


    private Handler handler = new Handler();

    private void finishAnimation() {
        finish();
        overridePendingTransition(0, R.anim.act_out);
    }
}
